package com.zhaosc.shiro.web.controller;

import java.lang.reflect.InvocationTargetException;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.zhaosc.shiro.model.Organization;
import com.zhaosc.shiro.service.OrganizationService;

/**
 * 
 * @Description:TODO
 * @author:zhaosc
 * @time:2017年8月24日
 * @exception
 */
@Controller
@RequestMapping("/organization")
public class OrganizationController {

	@Autowired
	private OrganizationService organizationService;
	
	@RequiresPermissions("organization:view")
	@RequestMapping(method=RequestMethod.GET)
	public String index(Model model){
		return "organization/index";
	}
	
    @RequiresPermissions("organization:view")
    @RequestMapping(value = "/tree", method = RequestMethod.GET)
    public String showTree(Model model) {
        model.addAttribute("organizationList", organizationService.findAll());
        return "organization/tree";
    }
    
    
    @RequiresPermissions("organization:create")
    @RequestMapping(value = "/{parentId}/appendChild", method = RequestMethod.GET)
    public String showAppendChildForm(@PathVariable("parentId") Long parentId, Model model) {
        Organization parent = organizationService.findOne(parentId);
        model.addAttribute("parent", parent);
        Organization child = new Organization();
        child.setParentId(parentId);
        child.setParentIds(parent.makeSelfAsParentIds());
        model.addAttribute("child", child);
        model.addAttribute("op", "新增");
        return "organization/appendChild";
    }
    
    @RequiresPermissions("organization:create")
    @RequestMapping(value = "/{parentId}/appendChild", method = RequestMethod.POST)
    public String create(Organization organization) {
        organizationService.createOrganization(organization);
        return "redirect:/organization/success";
    }
    
    @RequiresPermissions("organization:update")
    @RequestMapping(value = "/{sourceId}/move", method = RequestMethod.GET)
    public String showMoveForm(@PathVariable("sourceId") Long sourceId, Model model) {
        Organization source = organizationService.findOne(sourceId);
        model.addAttribute("source", source);
        model.addAttribute("targetList", organizationService.findAllWithExclude(source));
        return "organization/move";
    }
//    @RequiresPermissions("organization:update")
//    @RequestMapping(value="/{sourceId}/move",method=RequestMethod.POST)
//    public String move(@PathVariable("sourceId")Long sourceId,@RequestParam("targetId") Long targetId){
//		
//    	Organization source=organizationService.findOne(sourceId);
//    	Organization target=organizationService.findOne(targetId);
//    	organizationService.move(source, target);
//    	return "redirect:/organization/success";
//    }
    



    @RequiresPermissions("organization:update")
    @RequestMapping(value = "/{id}/maintain", method = RequestMethod.GET)
    public String showMaintain(@PathVariable(value="id") long id,Model model) {
        model.addAttribute("organization", organizationService.findOne(id));
        return "organization/maintain";
    }
    

    @RequiresPermissions("organization:delete")
    @RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
    public String deleteOrganization(@PathVariable(value="id") long id, RedirectAttributes redirectAttributes) {
    	 organizationService.deleteOrganization(id);
        redirectAttributes.addFlashAttribute("msg", "删除成功");
        return "redirect:/organization/success";
    }
    
    @RequiresPermissions("organization:update")
    @RequestMapping(value = "/{id}/update", method = RequestMethod.GET)
    public String updateOrganization(@PathVariable(value="id") long id, RedirectAttributes redirectAttributes) {
    	Organization organization= organizationService.findOne(id);
        redirectAttributes.addFlashAttribute("organization", organization);
        return "organization/maintain";
    }
    
    @RequiresPermissions("organization:update")
    @RequestMapping(value = "/{id}/update", method = RequestMethod.POST)
    public String update(Organization organization, RedirectAttributes redirectAttributes) {
        try {
			organizationService.updateOrganization(organization);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
        redirectAttributes.addFlashAttribute("msg", "修改成功");
        return "redirect:/organization/success";
    }
    
    @RequiresPermissions("organization:view")
    @RequestMapping(value = "/success", method = RequestMethod.GET)
    public String success() {
        return "organization/success";
    }

}
